//
//  UUHybridSelectFleetHandler.m
//  UUCharterSupplier
//
//  Created by ZhangAo on 26/01/2018.
//  Copyright © 2018 UUCharter. All rights reserved.
//

#import "UUHybridSelectFleetHandler.h"
#import "UUFleetPicker.h"
#import <UUFoundation/UIBarButtonItem+CustomButton.h>
#import "UUFleetManager.h"

@implementation UUHybridSelectFleetHandler

- (NSString *)name {
    return @"selectMotorcade";
}

- (void)executeWithParams:(id)params callbackWith:(void (^)(id))callback {
    NSMutableArray *selectedFleets = nil;
    NSString *fleetIdString = params[@"motorcadeId"];
    if ([fleetIdString isKindOfClass:[NSNumber class]]) {
        fleetIdString = [(NSNumber *)fleetIdString stringValue];
    }
    
    if (fleetIdString.length > 0) {
        selectedFleets = [[fleetIdString componentsSeparatedByString:@","] mutableCopy];
        [selectedFleets enumerateObjectsUsingBlock:^(NSString  *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [selectedFleets replaceObjectAtIndex:idx withObject:@([obj integerValue])];
        }];
    }

    BOOL multiple = [params[@"multiple"] boolValue];
    
    UIViewController *picker = [UUFleetPicker pickerWithSelectedFleetIds:selectedFleets
                                                 allowsMultipleSelection:multiple
                                                           completeBlock:^(NSArray<NSNumber *> *selectedFleetIds) {
                                                               NSMutableString *fleetIds = [NSMutableString new];
                                                               NSMutableString *fleetNames = [NSMutableString new];
                                                               for (NSNumber *fleetId in selectedFleetIds) {
                                                                   UUFleetModel *fleet = [GetFleetManager() fetchFleetWithFleetId:fleetId];
                                                                   if (fleet) {
                                                                       if (fleetIds.length > 0) {
                                                                           [fleetIds appendString:@","];
                                                                           [fleetNames appendString:@","];
                                                                       }
                                                                       
                                                                       [fleetIds appendString:[fleetId stringValue]];
                                                                       [fleetNames appendString:fleet.name];
                                                                   }
                                                               }
                                                               
                                                               callback(@{
                                                                          @"motorcade_id" : fleetIds,
                                                                          @"motorcade_name" : fleetNames,
                                                                          });
                                                           }];
    [self.context.hostVC presentViewController:picker
                                      animated:YES
                                    completion:nil];
}

@end
